#include "PrecompiledHeader_Engineer.h"
#include "WWater.h"

/*--------------------------------------------------------------------------*/

WWater::WWater()
{
	this->m_waterRenderable = NULL;
}

/*--------------------------------------------------------------------------*/

WWater::~WWater()
{

}

/*--------------------------------------------------------------------------*/

void WWater::OnLevelLoad(const std::string& level)
{
	this->InitWaterPlane();
}

/*--------------------------------------------------------------------------*/

void WWater::OnLevelUnload()
{
	delete this->m_waterVao;

	CTree::getInstance()->DeleteObject(this->m_waterRenderable);
}

/*--------------------------------------------------------------------------*/

void WWater::OnLevelCalc()
{

}

/*--------------------------------------------------------------------------*/

void WWater::InitWaterPlane()
{
	unsigned int width = 5;
	unsigned int height = 5;

	this->m_waterVao = new VertexArray(3);

	glm::vec3* verts = new glm::vec3[height * width];
	glm::vec3* normal = new glm::vec3[height * width];
	glm::vec2* texcor = new glm::vec2[height * width];

	int counter = 0;

	glm::vec2 tmp;

	for (unsigned int y = 0; y < height; ++y)
	{
		for (unsigned int x = 0; x < width; ++x)
		{
			verts[counter].x = (float) x / (float)(width - 1);
			verts[counter].y = (float) y / (float)(height - 1);
			verts[counter].z = 0.0f;

			texcor[counter].x = (float) x / (float)(width - 1);
			texcor[counter].y = (float) y / (float)(height - 1);

			normal[counter] = glm::vec3(0.0f, 0.0f, 1.0f);

			++counter;
		}
	}

	unsigned int* indices = new unsigned int[width * height + (width - 1) * (height - 2)];
	int c = 0;

	for (unsigned int row = 0; row < height - 1; ++row)
	{
		if ((row % 2) == 0)   // even rows
		{
			for (unsigned int col = 0; col < width; ++col)
			{
				indices[c++] = col + row * width;
				indices[c++] = col + (row + 1) * width;
			}
		}
		else   // odd row
		{
			for (unsigned int col = width - 1; col > 0; --col)
			{
				indices[c++] = col + (row + 1) * width;
				indices[c++] = col - 1 + row * width;
			}
		}
	}

	if ((height & 1) && height > 2)
	{
		indices[c] = (height - 1) * width;
	}

	this->m_waterVao->SetData((float*) verts, width * height, 0, 3);
	this->m_waterVao->SetData((float*) normal, width * height, 1, 3);
	this->m_waterVao->SetData((float*) texcor, width * height, 2, 2);

	this->m_waterVao->SetIndices(indices, width * height + (width - 1) * (height - 2), GL_TRIANGLE_STRIP);

	delete [] verts;
	delete [] texcor;
	delete [] normal;
	delete [] indices;

	this->m_waterRenderable = new RenderableVao();

	this->m_waterRenderable->SetLowQualityVao(this->m_waterVao);
	this->m_waterRenderable->SetHighQualityVao(this->m_waterVao);
	this->m_waterRenderable->SetOcclusionTestEnabled(false);

	this->m_waterRenderable->ClearTasks();
	this->m_waterRenderable->AddRenderTask(CContext::getInstance()->GetContext("d3d_water"),
	                                       CMaterial::getInstance()->GetMaterial("mat_3d_water"),
	                                       CCamera::getInstance()->GetCamera("cam3d"),
	                                       10,
	                                       vl::Render::ColorAndDepth,
	                                       9999);

	std::stringstream ss;
	ss << "water";
	this->m_waterRenderable->SetName(ss.str());

	this->m_waterRenderable->SetPosition(glm::vec3(0.0f, 0.0f, 3.0f));
	this->m_waterRenderable->SetScale(1000.0f);
	this->m_waterRenderable->SetRadius(1000.0f * glm::sqrt(2.0f));
	this->m_waterRenderable->SetCanBeActive(false);

	this->m_waterRenderable->SetParent(CTree::getInstance()->GetRoot());
}

/*--------------------------------------------------------------------------*/
